/*
 * author: Stngle
 * e-mail: 1113591198@qq.com
 */
 
// 获取一个websocket server对象
var WebSocketServer = require('ws').Server;
// 创建ws服务器
var ws = new WebSocketServer({
	port: 1234	// 监听的端口
});
// 设置发送给客户端的数据
var sendData = {
	info:[
		{
			id: "00000000",								
			time: "0:00--0:27",							
			imgSrc: "../images/00000001.png",	
			sort: "movie",			
			title:"微微一笑很倾城",					
			brief: "导演：赵天宇<br/>主演：Angelababy/井柏然/李沁/谭松韵/李现",	
			locationUrl: "http://www.baidu.com",
			collection: "favorite_border"										
		},
		{
			id: "00000001",								
			time: "0:00--0:27",							
			imgSrc: "../images/00000001.png",	
			sort: "movie",			
			title:"微微一笑很倾城",					
			brief: "导演：赵天宇<br/>主演：Angelababy/井柏然/李沁/谭松韵/李现",	
			locationUrl: "http://www.baidu.com",			
			collection: "favorite"									
		}
	]
};
// 将发送数据decode为JSON格式
var sendDataDecode = JSON.stringify(sendData);
// 处理连接
ws.on('connection',
	function(socket) {
		socket.onmessage = message;
		socket.onclose = close;
		socket.onerror = error;
		socket.onopen = open;
		socket.send(sendDataDecode, function() {
		console.log("data has send");
	})
	});

function message(msg){
    //对接收到的消息做些什么
}
function error(err){
	// 消息出错做什么
    console.log(err);
}
function close(){
	// 会话关闭做什么
    console.log("socket closed");
}
function open(){
	// 会话打开时
    console.log("socket opened");
}


